import { defineStore } from 'pinia'
import { reactive, ref } from 'vue'

export interface AutomationConfig {
  headless: boolean
  forceSelection: boolean
  forceDate: string
  forceTime: string
  unattended: boolean
  unattendedIntervalSeconds: number
  unattendedPerRecordSeconds: number
  unattendedScope: 'selected' | 'pending_failed'
  // 监控配置（刻度调度、中午固定窗口）
  scheduleEnabled: boolean
  scheduleMinutesInHour: number[]
  scheduleGuardSeconds: number
  middayReleaseEnabled: boolean
  middayReleaseWeekdays: number[]
  middayIntervalSeconds: number
  intervalInsideWindowSeconds: number
  intervalOutsideWindowSeconds: number
  maxSleepBeforeWindowSeconds: number
}

export const useAutomationStore = defineStore('automation', () => {
  const config = reactive<AutomationConfig>({
    headless: false,
    forceSelection: false,
    forceDate: '',
    forceTime: '',
    unattended: false,
    unattendedIntervalSeconds: 0,
    unattendedPerRecordSeconds: 5,
    unattendedScope: 'selected',
    // 监控配置默认值
    scheduleEnabled: true,
    scheduleMinutesInHour: [0, 15, 30, 45],
    scheduleGuardSeconds: 20,
    middayReleaseEnabled: true,
    middayReleaseWeekdays: [1, 2, 3, 4, 5],
    middayIntervalSeconds: 2,
    intervalInsideWindowSeconds: 5,
    intervalOutsideWindowSeconds: 600,
    maxSleepBeforeWindowSeconds: 900,
  })

  const selectedIds = ref<number[]>([])
  const isRunning = ref<boolean>(false)

  return { config, selectedIds, isRunning }
}, { persist: { paths: ['config', 'selectedIds', 'isRunning'] } })
